package com.micookie.v2ex.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.micookie.v2ex.R;
import com.micookie.v2ex.model.bean.Reply;
import com.micookie.v2ex.model.bean.Topic;
import com.micookie.v2ex.model.bean.TopicDetail;
import com.micookie.v2ex.utils.ToastUtils;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.OnImageClickListener;
import com.zzhoujay.richtext.callback.OnImageLongClickListener;
import com.zzhoujay.richtext.callback.OnUrlClickListener;
import com.zzhoujay.richtext.callback.OnUrlLongClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2016-10-25.
 */

public class TopicDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_HEADER = 0x01;
    public static final int TYPE_LIST = 0x02;

    private Context mContext;


    private TopicDetail mTopicDetail;
    private List<Reply> mReplyList;
    private Topic mTopic;
    private View mHeaderView;


    public TopicDetailAdapter(Context context, TopicDetail topicDetail) {
        this.mContext = context;
        this.mTopicDetail = topicDetail;
        this.mTopic = mTopicDetail.getTopic();
        this.mReplyList = mTopicDetail.getReplyList();

        initHeadView();
    }

    public void setHeaderAdapter() {
        notifyItemInserted(0);
    }


    public TopicDetailAdapter setTopicDetail(TopicDetail topicDetail) {
        this.mTopicDetail = topicDetail;
        this.mReplyList = mTopicDetail.getReplyList();
        this.mTopic = mTopicDetail.getTopic();
        return this;
    }

    public List<Reply> getmReplyList() {
        return mReplyList;
    }

    public TopicDetailAdapter setmReplyList(List<Reply> mReplyList) {
        this.mReplyList = mReplyList;
        return this;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return new HeaderViewHolder(mHeaderView);
        }

        return new ReplyViewHolder(LinearLayout.inflate(mContext, R.layout.item_topic_detail_reply, null));
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_LIST;
        if (position == 0) return TYPE_HEADER;
        return TYPE_LIST;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) {
            ((HeaderViewHolder) holder).setData(mTopic);
        } else {
            if (!mReplyList.isEmpty()) {
                ((ReplyViewHolder) holder).setData(mReplyList.get(position - 1));
            }
        }
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? mReplyList.size() : mReplyList.size() + 1;
    }


    private void initHeadView() {
        mHeaderView = LinearLayout.inflate(mContext, R.layout.item_topic_detail_header, null);
    }


    /**
     * ReplyViewHolder
     */
    class ReplyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.avatar_sdv)
        SimpleDraweeView mAvatarSdv;
        @BindView(R.id.name_tv)
        TextView mUserNameTv;
        @BindView(R.id.reply_content_tv)
        TextView mReplyContentTv;
        @BindView(R.id.otherInfo_tv)
        TextView mOtherInfoTv;
        @BindView(R.id.floorNum_tv)
        TextView mFloorNumTv;

        public ReplyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(Reply reply) {
            Uri uri = Uri.parse(reply.getMember().getAvatar());
            mAvatarSdv.setImageURI(uri);

            mReplyContentTv.setText(Html.fromHtml(reply.getContent()));
            mUserNameTv.setText(reply.getMember().getUsername());
            mFloorNumTv.setText(reply.getFloorNumString());
            mOtherInfoTv.setText(reply.getCreatedFormat());
        }
    }

    /**
     * HeaderViewHolder
     */
    class HeaderViewHolder extends RecyclerView.ViewHolder {
//        @BindView(R.id.header_title_tv)
//        TextView mTitleTv;
//        @BindView(R.id.header_username_tv)
//        TextView mUserNameTv;
//        @BindView(R.id.header_created_format_tv)
//        TextView mCreatedFormatTv;
//        @BindView(R.id.header_clicks_tv)
//        TextView mClicksTv;
        //        @BindView(R.id.header_content_webVi)
//        WebView mContentWebV;
        @BindView(R.id.header_content_tv)
        TextView mContentTv;
//        @BindView(R.id.header_avatar_sdv)
//        SimpleDraweeView mAvatarSdv;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(Topic topic) {
//            mUserNameTv.setText(topic.getMember().getUsername());
//            mCreatedFormatTv.setText(topic.getCreatedFormat());
//            mClicksTv.setText(topic.getClicks() + "次点击");
//            mTitleTv.setText(topic.getTitle());
            //setWebViewContent(topic.getContent());
            setContactTv(topic.getContent(), mContentTv);

//            Uri uri = Uri.parse(topic.getMember().getAvatar());
//            mAvatarSdv.setImageURI(uri);
        }


        private void setContactTv(String content, TextView textView) {
            RichText
                    .from(content) // 数据源
                    //  .type(RichText.TYPE_MARKDOWN) // 数据格式,不设置默认是Html,使用fromMarkdown的默认是Markdown格式
                    .autoFix(true) // 是否自动修复，默认true
                    //.async(true)  已取消异步的接口，交给调用者自己处理
//                    .fix(imageFixCallback) // 设置自定义修复图片宽高
//                    .fixLink(linkFixCallback) // 设置链接自定义回调
                    .noImage(false) // 不显示并且不加载图片
                    .resetSize(false) // 默认false，是否忽略img标签中的宽高尺寸（只在img标签中存在宽高时才有效），true：忽略标签中的尺寸并触发SIZE_READY回调，false：使用img标签中的宽高尺寸，不触发SIZE_READY回调
                    .clickable(true) // 是否可点击，默认只有设置了点击监听才可点击
                    .imageClick(imageClickListener) // 设置图片点击回调
                    .imageLongClick(imageLongClickListener) // 设置图片长按回调
                    .urlClick(urlClickListener) // 设置链接点击回调
                    .urlLongClick(urlLongClickListener) // 设置链接长按回调
//                    .placeHolder(placeHolder) // 设置加载中显示的占位图
//                    .error(errorImage) // 设置加载失败的错误图
                    .into(textView); // 设置目标TextView
        }

        private OnImageClickListener imageClickListener = new OnImageClickListener() {
            @Override
            public void imageClicked(List<String> imageUrls, int position) {
                ToastUtils.show("点击了图片" + imageUrls.get(position));
            }
        };

        private OnImageLongClickListener imageLongClickListener = new OnImageLongClickListener() {
            @Override
            public boolean imageLongClicked(List<String> imageUrls, int position) {
                ToastUtils.show("长按了图片" + imageUrls.get(position));
                return false;
            }
        };

        private OnUrlClickListener urlClickListener = new OnUrlClickListener() {
            @Override
            public boolean urlClicked(String url) {
                ToastUtils.show("点击了URL" + url);
                return false;
            }
        };

        private OnUrlLongClickListener urlLongClickListener = new OnUrlLongClickListener() {
            @Override
            public boolean urlLongClick(String url) {
                ToastUtils.show("长点击了URL" + url);
                return false;
            }
        };


    }
}
